/*
Purpose: calculate correlations between test scores and SES variables at state level
*/

use "data/clean/tempanal", clear

merge m:1 yob fips_birth using "data/clean/state_unemployment" // drops 2002 yob
keep if _merge==3
drop _merge

label var unemp_rate_age18 "State unemp at age 18"

merge m:1 yob fips_birth using "data/clean/med_income_age13_child"
drop if _merge==2
drop _merge

label var lnmed_income_age13_kid_sm "Ln med income at age 13 HH w/ kid smoothed"

merge m:1 yob fips_birth using "data/clean/birthweight_data"
drop if _merge==2
drop _merge
label var lowbirwt_pct "% of births with low birth weight"
label var vlowbirwt_pct "% of births with very low birth weight"

keep fips_birth yob fips_yob math08sd92_gap1 read08sd98_gap1 lnmed_income_age13_kid_sm unemp_rate_age18 lowbirwt_pct vlowbirwt_pct
duplicates drop

codebook fips_yob // all observations are unique FIPS/YOB pairs

gen math77 = math08sd92_gap1 if yob==1977
gen math83 = math08sd92_gap1 if yob==1983
gen math91 = math08sd92_gap1 if yob==1991
gen math00 = math08sd92_gap1 if yob==2000

egen math77st = mean(math77), by(fips_birth)
egen math83st = mean(math83), by(fips_birth)
egen math91st = mean(math91), by(fips_birth)
egen math00st = mean(math00), by(fips_birth)
gen mathlongdiff = math91st - math77st
gen mathlongdiff2 = math00st - math83st

gen read85 = read08sd98_gap1 if yob==1985
gen read00 = read08sd98_gap1 if yob==2000

egen read85st = mean(read85), by(fips_birth)
egen read00st = mean(read00), by(fips_birth)
gen readlongdiff = read00st - read85st

gen unemp77 = unemp_rate_age18 if yob==1977
gen unemp91 = unemp_rate_age18 if yob==1991

egen unemp77st = mean(unemp77), by(fips_birth)
egen unemp91st = mean(unemp91), by(fips_birth)
gen unemplongdiff = unemp91st - unemp77st

gen inc77_kid = lnmed_income_age13_kid_sm if yob==1977
gen inc91_kid = lnmed_income_age13_kid_sm if yob==1991
gen inc01_kid = lnmed_income_age13_kid_sm if yob==2001

egen inc77st_kid = mean(inc77_kid), by(fips_birth)
egen inc91st_kid = mean(inc91_kid), by(fips_birth)
egen inc01st_kid = mean(inc01_kid), by(fips_birth)
gen inclongdiff_kid = inc91st_kid - inc77st_kid

gen bwt77 = lowbirwt_pct if yob==1977
gen bwt91 = lowbirwt_pct if yob==1991

egen bwt77st = mean(bwt77), by(fips_birth)
egen bwt91st = mean(bwt91), by(fips_birth)
gen bwtlongdiff = bwt91st - bwt77st

keep fips_birth *diff* *st *st_*
duplicates drop
di _N

statastates, fips(fips_birth)
corr mathlongdiff2 readlongdiff
corr mathlongdiff unemplongdiff
corr mathlongdiff inclongdiff_kid
corr mathlongdiff bwtlongdiff